﻿using ImperialWebSite.Presenters.Status.Classes;
using ImperialWebSite.Presenters.Status.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ImperialWebSite.Views.Main.Status
{
    public partial class StatusRegistry : Page, IStatusRegistry
    {
        private StatusRegistryPresenter _presenter;
        private bool _successfulTransaction;

        public string StatusNameText
        {
            get { return StatusName.Text; }
        }

        public object SessionCurrentStatuses
        {
            get { return Session["currentStatuses"]; }
            set { Session["currentStatuses"] = value; }
        }


        public string TransactionMessage
        {
            get { return FailureText.Text; }
            set { FailureText.Text = value; }
        }

        public bool SuccessfulTransaction
        {
            get { return _successfulTransaction; }
            set { _successfulTransaction = value; }
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _presenter = new StatusRegistryPresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                InitComponents();
                LoadCurrentStatuses();
                btnRegister.Enabled = SessionCurrentStatuses != null;
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            LoadCurrentStatuses();
            InsertNewStatus();
        }



        private void InitComponents()
        {
            SuccessfulTransaction = false;
            TransactionMessage = string.Empty;
        }

        private void LoadCurrentStatuses()
        {
            _presenter.LoadCurrentStatuses();
        }

        private void InsertNewStatus()
        {
            if (StatusNameText != string.Empty)
            {
                _presenter.InsertNewStatus();
                if (_successfulTransaction)
                    CleanFields();
            }
        }

        private void CleanFields()
        {
            StatusName.Text = string.Empty;
        }
    }
}